package com.xm.xmapp.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.xm.xmapp.R;
import com.xm.xmapp.myinterface.DialogInterFace;

public class TipLeftRightDialog extends Dialog {

	private Context context;
	private String title_str;
	private String content_str;

	private TextView left, right;
	private String left_str, right_str;
	

	private DialogInterFace dialogInterFace;
	
	/**
	 * 0是免费预约次数用完1是取消预约2是更新下载按钮，此时只有一个确定按钮
	 */
	private int flag;

	public TipLeftRightDialog(Context context, String title, String content, int flag) {
		super(context, R.style.Dialog);
		this.context = context;
		this.title_str = title;
		this.content_str = content;
		this.flag = flag;

	}

	public void init() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.dialog_tip_left_right, null);
		TextView title = (TextView) view.findViewById(R.id.title);
		TextView content = (TextView) view.findViewById(R.id.content);
		left = (TextView) view.findViewById(R.id.left);
		right = (TextView) view.findViewById(R.id.right);

		title.setText(title_str == null ? "" : title_str);
		content.setText(content_str == null ? "" : content_str);
		left.setText(left_str == null ? "" : left_str);
		right.setText(right_str == null ? "" : right_str);

		left.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (dialogInterFace != null)
					dialogInterFace.left(flag,"");
			}
		});

		right.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (dialogInterFace != null)
					dialogInterFace.right(flag,"");
			}
		});
		
		if(flag == 2) {
			left.setVisibility(View.GONE);
		}

		setContentView(view);

		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
		lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
		dialogWindow.setAttributes(lp);

	}

	public TipLeftRightDialog setDialogInterFace(String left,String right,
			DialogInterFace dialogInterFace) {
		this.dialogInterFace = dialogInterFace;
		this.left_str = left;
		this.right_str = right;
		return this;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}
}
